package cn.lsc.web.controller;

import cn.lsc.domain.CourseType;
import cn.lsc.dto.CrumbsDto;
import cn.lsc.query.CourseTypeQuery;
import cn.lsc.result.JSONResult;
import cn.lsc.result.PageList;
import cn.lsc.service.ICourseTypeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courseType")
public class CourseTypeController {

    @Autowired
    public ICourseTypeService courseTypeService;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody CourseType courseType) {
        if (courseType.getId() != null) {
            courseTypeService.updateById(courseType);
        } else {
            courseTypeService.insert(courseType);
        }
        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        courseTypeService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(courseTypeService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(courseTypeService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseTypeQuery query) {
        Page<CourseType> page = new Page<CourseType>(query.getPage(), query.getRows());

        EntityWrapper<CourseType> wrapper = new EntityWrapper<>();
        wrapper.like(!StringUtils.isEmpty(query.getKeyword()), "name", query.getKeyword());
        page = courseTypeService.selectPage(page, wrapper);
        //判断是父级
        page.getRecords().forEach(e -> {
            if (e.getPid() != null) {
                e.setParent(e.selectById(e.getPid()));
            }
        });
        return JSONResult.success(new PageList<CourseType>(page.getTotal(), page.getRecords()));
    }

    @GetMapping("/treeData")
    public JSONResult treeData() {
        return JSONResult.success(courseTypeService.treeData());
    }

    @PatchMapping
    public JSONResult patchDel(@RequestBody List<Long> ids) {
        courseTypeService.patchDel(ids);
        return JSONResult.success();
    }
    @GetMapping("/crumbs/{courseTypeId}")
    public JSONResult crumbs(@PathVariable("courseTypeId")Long courseTypeId ) {
        List<CrumbsDto> crumbs = courseTypeService.crumbs(courseTypeId);
        return JSONResult.success(crumbs);
    }
}
